package com.stylefeng.guns.modular.finance.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.finance.service.*;
import com.stylefeng.guns.modular.system.model.CostChannel;
import com.stylefeng.guns.modular.system.model.CostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 渠道推广费控制器
 *
 * @author fengshuonan
 * @Date 2018-08-26 15:55:16
 */
@Controller
@RequestMapping("/costTotal")
public class CostTotalController extends BaseController {

    private String PREFIX = "/finance/costTotal/";

    @Autowired
    private ICostLoanService costLoanService;

    @Autowired
    private ICostOnlineLoanService costOnlineLoanService;

    @Autowired
    private ICostChannelService costChannelService;

    @Autowired
    private ICostChannelSuperService costChannelSuperService;

    @Autowired
    private ICostServiceService costServiceService;

    /**
     *
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "costTotal.html";
    }

    /**
     *
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object outlayList(String condition) {
        List<Map<String, Object>> mapList = null;
        if (condition.equals("收入")) {
            mapList = costLoanService.selectMaps(null);
            mapList.addAll(costOnlineLoanService.selectMaps(null));
            BigDecimal total = new BigDecimal(0.00);
            for (Map<String, Object> map: mapList) {
                if (map.get("costType").equals(condition)) {
                    total = total.add((BigDecimal) map.get("amount"));
                }
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("amount", total);
            mapList.add(0, map);
            return mapList;
        }
        if (condition.equals("支出")) {
            mapList = costServiceService.selectMaps(null);
            mapList.addAll(costChannelSuperService.selectMaps(null));
            BigDecimal total = new BigDecimal(0.00);
            for (Map<String, Object> map : mapList) {
                if (!map.get("payType").equals("投放消费")) {
                    total = total.add((BigDecimal) map.get("amount"));
                }
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("amount", total);
            mapList.add(0, map);
            return mapList;
        }
        return null;
    }

    /**
     * 获取渠道推广费列表
     */
//    @RequestMapping(value = "/incomeList")
//    @ResponseBody
//    public Object incomeList(String condition) {
//        return costChannelService.selectList(null);
//    }




}
